//更新时间 2024.11.4
const LAOLENG_IOS_TJ_SWIFT_PLUGIN_VERSION = "1.0.5"
const plugName = "laolengIOSTJ"
const clzName = "LaoLengPlugin"

/**
 * @description 老冷IOS脱机swift插件
 */
class LaolengIOS_TJ_SWIFT {
    constructor() {
        this.isDebug = true
        if (!pluginLoader.loadPlugin(plugName)) {
            loge("载入插件失败: " + pluginLoader.getErrorMsg())
            if (!this.isDebug) exit()
            return
        }
        if (pluginLoader.makeInstance(plugName, clzName)) {
            loge("实例化类失败: " + clzName)
            if (!this.isDebug) exit()
            return false
        }
        this.version = pluginLoader.callMethodStr(plugName, clzName, "Ver", "")
        logd(this.version)
        logw("LaolengIOS_TJ_SWIFT插件ver:{},加载成功,EC教学咨询,老冷编程学院:http://bbs.laoleng.vip", LAOLENG_IOS_TJ_SWIFT_PLUGIN_VERSION)
        if (this.version !== LAOLENG_IOS_TJ_SWIFT_PLUGIN_VERSION) {
            loge("注意!!!LaolengIOS_TJ_SWIFT版本{}" +
                "与laolengIOSTJ.framework版本{}不符,如遇报错\n" +
                "到老冷VIP论坛http://bbs.laoleng.vip/的IOS脱机VIP板块[laoleng-swift插件更新]" +
                "获取最新版替换", LAOLENG_IOS_TJ_SWIFT_PLUGIN_VERSION, this.version)
        }
        this.app = new LL_App()
        this.crypto = new LL_Crypto()
        this.device = new LL_Device()
        this.file = new LL_File()
        this.qrcode = new LL_QRcode()
        this.notification = new LL_Notification()
        this.photo = new LL_Photos()
    }
}

/**
 * @description app相关
 */
class LL_App {
    constructor() {
        this.methodName = "App"
    }

    /**
     * @description 获取脚本自身BundleId
     * @returns {string}脚本自身BundleId
     */
    getSelfBundleId() {
        let args = JSON.stringify({funcName: "getSelfBundleId"})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }

    /**
     * @description 获取脚本自身app名
     * @returns {string}脚本自身app名
     */
    getSelfName() {
        let args = JSON.stringify({funcName: "getSelfName"})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }
}

/**
 * @description crypto加密相关
 */
class LL_Crypto {
    constructor() {
        this.methodName = "Crypto"
    }

    /**
     * 获取字符串MD5
     * @param str {string} 待加密字符串
     * @returns {string} 加密后的MD5字符串
     */
    MD5(str) {
        let args = JSON.stringify({funcName: "md5", args1: str})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }

    /**
     * 获取字符串SHA256
     * @param str {string} 待加密字符串
     * @returns {string} 加密后的SHA256字符串
     */
    SHA256(str) {
        let args = JSON.stringify({funcName: "sha256", args1: str})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }
}

/**
 * @description device设备相关
 */
class LL_Device {
    constructor() {
        this.methodName = "Device"
    }

    /**
     * 获取wifi的ipv4地址
     * @returns {string} wifi的ipv4地址
     */
    getWifiIP() {
        let args = JSON.stringify({funcName: "getWifiIP"})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }

    /**
     * 获取手机型号
     * @returns {string} 手机型号
     */
    getDeviceModel() {
        let args = JSON.stringify({funcName: "getDeviceModel"})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }

    /**
     * 获取脚本内存占用
     * @returns {string} MB
     */
    getAppMemoryUsage() {
        let args = JSON.stringify({funcName: "getAppMemoryUsage"})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }

    /**
     * 获取手机总内存
     * @returns {string} MB
     */
    getTotalRAM() {
        let args = JSON.stringify({funcName: "getTotalRAM"})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }

    /**
     * 获取脚本内存占用总内存百分比
     * @returns {string} MB
     */
    getMemoryUsagePercentage() {
        let args = JSON.stringify({funcName: "getMemoryUsagePercentage"})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }

    /**
     * 获取手机剩余内存
     * @returns {string} MB
     */
    getFreeMemory() {
        let args = JSON.stringify({funcName: "getFreeMemory"})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }
}

/**
 * @description file文件相关
 */
class LL_File {
    constructor() {
        this.methodName = "File"
    }

    /**
     * 读取plist文件,返回json
     * @param path {string} 文件路径
     * @returns {string} json字符串
     */
    readPlist(path) {
        if (!file.exists(path)) {
            loge("文件不存在: " + path)
            return ""
        }
        let args = JSON.stringify({funcName: "readPlist", args1: path})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }

    /**
     * 写入json字符串到plist文件
     * @param path {string} 文件路径
     * @param data {string} json字符串
     * @returns {boolean} 是否成功
     */
    writePlist(path, data) {
        let args = JSON.stringify({funcName: "writePlist", args1: data, args2: path})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args) === "1"
    }

    /**
     * 读取ASCII格式的源文件内容
     * @param path {string} 文件路径
     * @returns {string} 文件内容
     */
    readFileASCII(path) {
        if (!file.exists(path)) {
            loge("文件不存在: " + path)
            return ""
        }
        let args = JSON.stringify({funcName: "readFileASCII", args1: path})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }
}

/**
 * @description qrcode 二维码相关
 */
class LL_QRcode {
    constructor() {
        this.methodName = "QRcode"
    }

    /**
     * 识别图片UIImage数据中的二维码
     * @param UIImage {UIImage} UIImage格式的图片数据
     * @returns {string} 识别到的二维码内容
     */
    detectQRCode(UIImage) {
        let args = JSON.stringify({methodName: this.methodName, funcName: "detectQRCode"})
        return pluginLoader.callMethodAny(plugName, clzName, args, UIImage)
    }

    /**
     * 识别指定路径图片的二维码
     * @param path {string} 图片路径
     * @returns {string} 识别到的二维码内容
     */
    detectQRCodeByPath(path) {
        if (!file.exists(path)) {
            loge("文件不存在: " + path)
            return ""
        }
        let args = JSON.stringify({methodName: this.methodName, funcName: "detectQRCodeByPath", args1: path})

        return pluginLoader.callMethodAny(plugName, clzName, args, "")
    }
}

/**
 * @description 通知栏相关
 */
class LL_Notification {
    constructor() {
        this.methodName = "Notification"
    }

    /**
     * 请求通知栏权限
     * @returns {*}
     */
    requestNotificationAuthorization() {
        let args = JSON.stringify({funcName: "requestNotificationAuthorization"})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }

    /**
     * @description 发送后台通知栏消息
     * @param msg {string} 通知栏内容
     * @param title {string?} 通知栏标题,可空,不填默认为脚本名
     * @param requestIdentifier{string} 请求id,可空,不修改则默认覆盖原信息
     * @returns {*}
     */
    sendLocalNotification(msg, title = "", requestIdentifier = "laolengSwiftPlug") {
        let args = JSON.stringify({
            funcName: "sendLocalNotification",
            args1: title,
            args2: msg,
            args3: requestIdentifier
        })
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }

}

/**
 * @description photo 相册相关
 */
class LL_Photos {
    constructor() {
        this.methodName = "Photos"
    }

    /**
     * 请求相册权限
     */
    requestPhotoAuthorization() {
        let args = JSON.stringify({funcName: "requestPhotoAuthorization"})
        return pluginLoader.callMethodStr(plugName, clzName, this.methodName, args)
    }

    /**
     * 保存uiimage格式图片到相册指定相簿
     * @param UIImage{UIImage} UIImage格式的图片数据
     * @param albumName {string} 相簿名,没有会自动创建
     * @returns {*}
     */
    saveUIImage(UIImage, albumName) {
        let args = JSON.stringify({methodName: this.methodName, funcName: "saveImage", args1: albumName})
        return pluginLoader.callMethodAny(plugName, clzName, args, UIImage)
    }

    /**
     * 异步删除所有图片,需用户确认
     * @returns {*}
     */
    deleteAllPhotos() {
        let args = JSON.stringify({methodName: this.methodName, funcName: "deleteAllPhotos"})
        return pluginLoader.callMethodAny(plugName, clzName, args, "")
    }
}

const ll = new LaolengIOS_TJ_SWIFT()

